<template>
	<view class="login">
		<view class="box">
			<image src="../../static/images/logo.png" mode="aspectFit"></image>
			<p>空港卡券联盟</p>
		</view>
		<!-- <p>申请获得你的公开信息(昵称 头像等)</p> -->
		<u-checkbox-group class="u-checkbox-group">
			<u-checkbox class="u-checkbox" @change="checkboxChange" v-model="item.checked" v-for="(item, index) in list" :key="index">
			</u-checkbox>
			<text class="text-one">已阅读并同意此</text>
			<text @click="getUserServiceAgreement">服务协议</text>
		</u-checkbox-group>
		<button :disabled="disabled2" type="button" @click="logingo" :class="state == 1 ? 'active' : 'noActive'">微信用户一键登录</button>
		<u-modal v-model="show" v-html="content" :content="content" :title="title" :content-style="{padding: '30rpx'}"></u-modal>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {shareMixins} from '../../mixins/share.js'
	export default {
		mixins:[shareMixins],
		data() {
			return {
				shareData: {
				    title: '空港卡券联盟商户端',
				    path: '/pages/mine/mine' // 分享的页面路径
				},
				code:'',
				openid:'',
				list: [{
					name: '已阅读并同意此',
					checked: false,
					disabled: false
				}],
				title:'',
				show: false,
				content:'',
				disabled2: true,
				state:0
			}
		},
		onLoad() {},
		methods: {	
			logingo(){
				var _this = this
				uni.getUserProfile({
					desc:'登录',
					lang:'zh_CN',
					success(res){
						if(res.userInfo){
							uni.setStorageSync('loginuserInfo',res.userInfo)
							uni.login({
							  provider: 'weixin',
							  success: (res) => {
								_this.code = res.code
								uni.request({
									url:'https://ca.wupin.shop/api/businessBasic/weChatBusinessLogin?code=' + _this.code,
									method:'GET',
									success: (res) => {
										console.log(res.data.data.company_name)
										if(res.data.code === 200 && res.data.data.company_name != '' && res.data.data.company_name != null){
											uni.setStorageSync('openid',res.data.data.openid)
											uni.switchTab({
												url:'../mine/mine'
											})
										}else if(res.data.code != 200){
											this.$refs.uToast.show({
												title:res.data.msg,
												type:'error'
											})
										}else if(res.data.data.company_name == '' || res.data.data.company_name == null || res.data.data.company_name == undefined){
											uni.setStorageSync('openid',res.data.data.openid)
											uni.reLaunch({
												url:'../firstlogin/firstlogin'
											})
										}
										
									}
								})
								
							  }
							});
						}
					},
					fail(err){
						console.log(err)
						console.log('拒绝了授权')
						uni.showModal({
						    title: '提示',	
						    content: '拒绝后无法正常浏览',
						    success: function (res) {
						        if (res.confirm) {
						            console.log('用户点击确定');
						        } else if (res.cancel) {
						            console.log('用户点击取消');
						        }
						    }
						});
					}
				})
			},
			// 选中某个复选框时，由checkbox时触发
			checkboxChange(e) {
				// console.log(e);
				if(e.value === true){
					this.disabled2 = false
					this.state = 1
				}else{
					this.disabled2 = true
					this.state = 0
				}
			},
			async getUserServiceAgreement(){//获取服务协议
				const res = this.$myRequest({
					url:'/api/common/getUserServiceAgreement'
				}).then((res) => {
					this.show = true
					this.content = res.data.data.setting_content
					this.title = res.data.data.setting_key
				})
			}
		},
	}
</script>

<style lang="scss">
	.login{
		.u-checkbox-group{
			margin-top: 50rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			.u-checkbox{
				margin: 0;
				padding: 0;
			}
			text{
				font-size: 30rpx;
				color: #2B85E4;
			}
			.text-one{
				margin-left: -20rpx;
				margin-right: 10rpx;
				font-size: 30rpx;
				color: #333333;
			}
		}
		p{
			text-align: center;
			margin-top: 50rpx;
			color: #909AB1;
		}
		.box{
			width: 260rpx;
			height: auto;
			margin: auto;
			margin-top: 200rpx;
			text-align: center;
			image{
				width: 163rpx;
				height: 130rpx;
				margin-bottom: 22rpx;
			}
			p{
				font-size: 40rpx;
				color: #3C3C3C;
				
			}
		}
		.active{
			display: flex;
			align-items: center;
			justify-content: center;
			width: 80%;
			height: 100rpx;
			background: linear-gradient(#FFB487 , #EC5F2F);
			border-radius: 100rpx;
			color: #FFFFFF;
			font-size: 36rpx;
			margin-top: 20rpx;
		}
		.noActive {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 80%;
			height: 100rpx;
			background: linear-gradient(#BCBCBC, #808080);
			border-radius: 100rpx;
			color: #FFFFFF;
			font-size: 36rpx;
			margin-top: 20rpx;
		}
	}
</style>
